package com.yf.notify.modules.notify.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yf.base.api.api.dto.PagingReqDTO;
import com.yf.base.utils.BeanMapper;
import com.yf.notify.enums.MsgType;
import com.yf.notify.modules.notify.dto.MsgReadDTO;
import com.yf.notify.modules.notify.entity.MsgRead;
import com.yf.notify.modules.notify.mapper.MsgReadMapper;
import com.yf.notify.modules.notify.service.MsgReadService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* <p>
* 消息记录业务实现类
* </p>
*
* @author 聪明笨狗
* @since 2022-11-08 10:54
*/
@Service
public class MsgReadServiceImpl extends ServiceImpl<MsgReadMapper, MsgRead> implements MsgReadService {

    @Override
    public IPage<MsgReadDTO> paging(PagingReqDTO<MsgReadDTO> reqDTO) {

        //查询条件
        QueryWrapper<MsgRead> wrapper = new QueryWrapper<>();

        // 请求参数
        MsgReadDTO params = reqDTO.getParams();

        if(params!=null){
            if(StringUtils.isNotBlank(params.getMsgId())){
                wrapper.lambda().eq(MsgRead::getMsgId, params.getMsgId());
            }

            if(StringUtils.isNotBlank(params.getReceiver())){
                wrapper.lambda().like(MsgRead::getReceiver, params.getReceiver());
            }

            if(params.getReadState()!=null){
                wrapper.lambda().eq(MsgRead::getReadState, params.getReadState());
            }
        }

        //获得数据
        IPage<MsgRead> page = this.page(reqDTO.toPage(), wrapper);
        //转换结果
        IPage<MsgReadDTO> pageData = JSON.parseObject(JSON.toJSONString(page), new TypeReference<Page<MsgReadDTO>>(){});
        return pageData;
    }


    @Override
    public void save(MsgReadDTO reqDTO){
        //复制参数
        MsgRead entity = new MsgRead();
        BeanMapper.copy(reqDTO, entity);
        this.saveOrUpdate(entity);
    }

    @Override
    public void delete(List<String> ids){
        //批量删除
        this.removeByIds(ids);
    }

    @Override
    public MsgReadDTO detail(String id){
        MsgRead entity = this.getById(id);
        MsgReadDTO dto = new MsgReadDTO();
        BeanMapper.copy(entity, dto);
        return dto;
    }

    @Override
    public List<MsgReadDTO> list(MsgReadDTO reqDTO){

        //分页查询并转换
        QueryWrapper<MsgRead> wrapper = new QueryWrapper<>();

        //转换并返回
        List<MsgRead> list = this.list(wrapper);

        //转换数据
        List<MsgReadDTO> dtoList = BeanMapper.mapList(list, MsgReadDTO.class);

        return dtoList;
    }

    @Override
    public List<MsgRead> listByMsg(String msgId) {
        //分页查询并转换
        QueryWrapper<MsgRead> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(MsgRead::getMsgId, msgId);
        //转换并返回
        List<MsgRead> list = this.list(wrapper);
        return list;
    }

    @Override
    public void markRead(String msgId, String userId) {
        QueryWrapper<MsgRead> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(MsgRead::getMsgId, msgId)
                .eq(MsgRead::getUserId, userId);

        MsgRead msg = this.getOne(wrapper, false);
        if(msg == null){
            msg = new MsgRead();
            // 只有站内信才会有此业务逻辑
            msg.setMsgType(MsgType.IM);
            msg.setMsgId(msgId);
            msg.setUserId(userId);
            msg.setReceiver(userId);
        }
        msg.setReadState(1);
        msg.setReadTime(new Date());
        this.saveOrUpdate(msg);
    }

    @Override
    public void removeByMsg(List<String> msgIds) {
        QueryWrapper<MsgRead> wrapper = new QueryWrapper<>();
        wrapper.lambda().in(MsgRead::getMsgId, msgIds);
        this.remove(wrapper);
    }

    @Override
    public int countUnread(String userId) {
        QueryWrapper<MsgRead> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(MsgRead::getUserId, userId)
                .eq(MsgRead::getMsgType, MsgType.IM)
                .eq(MsgRead::getReadState, 0);
        return this.count(wrapper);
    }

    @Override
    public void markAsRead(String userId, List<String> ids) {
        QueryWrapper<MsgRead> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(MsgRead::getUserId, userId)
                .in(MsgRead::getMsgId, ids);

        MsgRead read = new MsgRead();
        read.setReadState(1);
        this.update(read, wrapper);
    }

    @Override
    public void userDelete(String userId, List<String> ids) {
        QueryWrapper<MsgRead> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(MsgRead::getUserId, userId)
                .in(MsgRead::getMsgId, ids);
        this.remove(wrapper);
    }
}
